package com.sheng.leetcode.year2022.swordfingeroffer.day13;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author liusheng
 * @date 2022/09/13
 *<p>
 * 剑指 Offer 57. 和为s的两个数字<p>
 *<p>
 * 输入一个递增排序的数组和一个数字s，在数组中查找两个数，使得它们的和正好是s。如果有多对数字的和等于s，则输出任意一对即可。<p>
 *<p>
 * 示例 1：<p>
 * 输入：nums = [2,7,11,15], target = 9<p>
 * 输出：[2,7] 或者 [7,2]<p>
 *<p>
 * 示例 2：<p>
 * 输入：nums = [10,26,30,31,47,60], target = 40<p>
 * 输出：[10,30] 或者 [30,10]<p>
 *<p>
 * 限制：<p>
 *<p>
 * 1 <= nums.length <= 10^5<p>
 * 1 <= nums[i]<= 10^6<p>
 *<p>
 * 来源：力扣（LeetCode）<p>
 * 链接：<a href="https://leetcode.cn/problems/he-wei-sde-liang-ge-shu-zi-lcof">...</a><p>
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。<p>
 */
public class Sword0057 {

    @Test
    public void test01() {
//        int[] nums = {2,7,11,15};
//        int target = 9;
        int[] nums = {10,26,30,31,47,60};
        int target = 40;
        System.out.println(Arrays.toString(new Solution57().twoSum(nums, target)));
    }
}
class Solution57 {
    public int[] twoSum(int[] nums, int target) {
        int length = nums.length;
        int[] ints = new int[2];
        int left = 0;
        int right = length - 1;
        while (left < right) {
            if (nums[left] + nums[right] > target) {
                right--;
            } else if (nums[left] + nums[right] < target) {
                left++;
            } else {
                ints[0] = nums[left];
                ints[1] = nums[right];
                return ints;
            }
        }
        return new int[2];
    }
}
